const net = require("net");
const {getOrigin, getLocalPeerSocketAddress} = require("./State");
const ImageResponsePacket = require("./image-socket/ImageResponsePacket");
const Singleton = require("./Singleton");

function responseOrigin(image_data) {
    let origin = getOrigin();
    let local_peer_socket_address = getLocalPeerSocketAddress();

    let client = net.createConnection({
        host: origin.ip,
        port: origin.port,
        localPort: local_peer_socket_address.port,
        localAddress: local_peer_socket_address.ip
    });
    client.on('connect', function () {
       let p = ImageResponsePacket.build(7,
           1,
           Singleton.getSequenceNumber(),
           Singleton.getTimestamp(),
           image_data
       );
       client.write(p);
    });

}

module.exports = {
    responseOrigin
}